package com.kyle.redpacket.dto.result;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ALL")
public enum ResultEnum {
    DATABASE_SAVE_ERROR(-1, "数据库保存"),
    UNKONW_ERROR(0, "未知错误"),
    SUCCESS(1, "成功"),

    RUN_OUT_OF_MONEY(2,"红包被抢完")
    ;
    private final Integer code;

    private final String msg;

    ResultEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static String getMsg(Integer code) {
        for (ResultEnum item : ResultEnum.values()) {
            if (item.code.equals(code)) {
                return item.msg;
            }
        }
        return UNKONW_ERROR.msg;
    }


    public static String getMsg(String name) {
        for (ResultEnum item : ResultEnum.values()) {
            if (item.name().equals(name)) {
                return item.msg;
            }
        }
        return name;
    }
    public static ResultEnum getEnum(Integer code) {
        for (ResultEnum item : ResultEnum.values()) {
            if (item.code.equals(code)) {
                return item;
            }
        }
        return null;
    }

    //校验重复的code值
    public static void main(String[] args) {
        ResultEnum[] ApiResultCodes = ResultEnum.values();
        List<Integer> codeList = new ArrayList<Integer>();
        for (ResultEnum ApiResultCode : ApiResultCodes) {
            if (codeList.contains(ApiResultCode.code)) {
                System.out.println(ApiResultCode.code);
            } else {
                codeList.add(ApiResultCode.getCode());
            }
        }
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return this.name();
    }
}